package com.yowasa.xiuxian.engine.battle.strategy.drop.all;

import cn.hutool.core.util.RandomUtil;
import com.yowasa.xiuxian.annoations.BS;
import com.yowasa.xiuxian.constant.XiuxianConstants;
import com.yowasa.xiuxian.engine.battle.strategy.BaseSpellStrategy;
import com.yowasa.xiuxian.entity.bo.battle.BattleContent;
import com.yowasa.xiuxian.entity.bo.battle.BuffContent;
import com.yowasa.xiuxian.entity.bo.battle.UserBattleContent;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ElementEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.service.helper.BattleHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;

@BS(name = "涅槃劫火")
@Order(502)
@Component
public class NiePanJieHuo extends BaseSpellStrategy {
    @Autowired
    private BattleHelper battleHelper;

    @Override
    public ElementEnum getElement() {
        return ElementEnum.FIRE;
    }

    @Override
    public void action(UserBattleContent first, UserBattleContent second, BattleContent content) {
        BuffContent leiHuo = first.getBuff().get("法术虚弱");
        if (leiHuo == null) {
            BuffContent buffContent = new BuffContent();
            buffContent.setDuration(6);
            buffContent.setNum(1);
            first.getBuff().put("法术虚弱", buffContent);
        }else {
            leiHuo.setDuration(6);
            leiHuo.setNum(1);
        }
        int atk = first.getBattleInfo().getSpell();
        int def = second.getBattleInfo().getRes();
        int dmg = 0;
        for (int i = 0;i<5;i++) {
            double random = 4 + Math.random() * 4;
            dmg += (int) ((battleHelper.calSpell(atk, def)/10) * random);
        }

        int costMp = first.getBattleInfo().getMp() / 10;
        costMp = Math.max(costMp, 1000);
        first.getBattleInfo().setCmp(first.getBattleInfo().getCmp() - costMp);
        battleHelper.autoAction(this, first, second, content, dmg, "涅槃劫火");
        first.getCd().put("涅槃劫火", 5);
    }

    @Override
    public void limit(UserBO user) {
        if (user.getStage() < 5) {
            throw new HandlerException("学习/使用涅槃劫火阶位需要在如曜之金以上");
        }
        if (user.getElement().get("火") < XiuxianConstants.DEFAULT_ELEMENT_ACTIVITY) {
            throw new HandlerException("学习/使用涅槃劫火必须具有火灵根");
        }
    }

    @Override
    public String limitDesc() {
        return "学习/使用涅槃劫火阶位需要在如曜之金以上,且具有火灵根";
    }

    @Override
    public String desc() {
        return "释放五道火焰，每道火焰有0.4-0.8的倍率\ncd5回合，消耗最低1000点，最高10%的MP造成法术伤害\n释放之后会进入2回合的虚弱状态(无法解除)，法攻降低50%";
    }

    @Override
    public boolean check(UserBattleContent user, BattleContent content) {
        if(content.getTurn() <= 3){
            return false;
        }
        Integer cd = user.getCd().get("涅槃劫火");
        if (cd != null && cd > 0) {
            return false;
        }
        int costMp = user.getBattleInfo().getMp() / 10;
        costMp = Math.max(costMp, 1000);
        if (user.getBattleInfo().getCmp() < costMp) {
            return false;
        }
        return true;
    }
}
